#!/bin/bash
#
# A hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.

#
# Checks provided by the hook template
#

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=$(git hash-object -t tree /dev/null)
fi

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --bool hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
	# Note that the use of brackets around a tr range is ok here, (it's
	# even required, for portability to Solaris 10's /usr/bin/tr), since
	# the square bracket bytes happen to fall in the designated range.
	test $(git diff --cached --name-only --diff-filter=A -z $against |
	  LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
	cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

  git config hooks.allownonascii true
EOF
	exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached $against --

#
# Check formating of the commited .po files
#

# Get the list of updated po files
po_files=`git diff --cached --name-only --diff-filter=ACM | grep '\.po$'`
# Run 'msgfmt -vc' on each po file
# Warn about each malformed file and abort the commit if needed
failure=0
for po_file in $po_files
do
	msgfmt -vc $po_file > /dev/null 2>&1
	if [ $? -eq 1 ]; then
		echo "Error: Malformed file $po_file"
		echo "\tPlease run 'msgfmt -vc $po_file' and fix the reported errors."
		failure=1
	fi
done

# Exit only in case of error.
if [ $failure -eq 1 ]; then
	exit $failure
fi

#
# Sending of [DONE] messages
#

CONFIG_FILE=hooks/pre-commit.conf
# Bump this version number whenever a new, non-retrocompatible change
# is introduced in the configuration.
PRECOMMIT_SCRIPT_VERSION=1
if [ -f "$CONFIG_FILE" ]; then
	. "$CONFIG_FILE"
	if [ $PRECOMMIT_SCRIPT_VERSION -ne $PRECOMMIT_CONFIG_VERSION ]; then
		echo "Mismatch between the script and your configuration file."
		echo "Please update your configuration according to the template and update the value of \$PRECOMMIT_CONFIG_VERSION to $PRECOMMIT_SCRIPT_VERSION."
		exit 1
	fi

	# Prepare [DONE] messages
	# First, collect the message-ids of the [RFR] or [LCFC] messages matching the files.
	# Note: One message can be about several files, so we have to gather them.
	content=$(curl -L --silent $COORDINATION_URL)
	declare -A messages
	for po_file in $po_files
	do
		name=$(echo "$po_file" | sed -E 's#po/.+/##' | sed -E 's/\..*//')
		msgurl=$(echo "$content" | grep -A 6 "<td>manpages" | grep -A 4 "<td>$name/" | grep -o "https.*html")
		if [ -z "$msgurl" ]
		then
			echo "$po_file has not been found on $COORDINATION_URL"
		else
			msg=$(curl -L --silent $msgurl)
			msgid=$(echo "$msg" | grep "Message-id" | grep -oP '"/msgid-search/.*?"' | sed 's#"/msgid-search/\(.*\)"#\1#')
			title=$(echo "$msg" | grep h1 | sed -E 's/<.?h1>//g' | sed -E 's/\[.*\]//')
			messages[$msgid]=$title
		fi
	done

	# Now, for each collected message, send a [DONE] reply to the l10n list.
	for msgid in "${!messages[@]}"
	do
		case $MUA in
		mailto)
			# Escape line endings so they can be correctly interpreted by the MUA.
			escaped_done_msg=$(echo "$DONE_MSG" | sed ':a;N;$!ba;s/\n/%0D%0A/g')
			echo -e 'Ctrl+click on \e]8;;mailto:'"${LIST_ADDRESS}"'?subject=[DONE] '"${messages[$msgid]}"'&in-reply-to=<'"$msgid"'>&body='"$escaped_done_msg"'\e\\this link\e]8;;\e\\ to send your message.'
			;;
		thunderbird)
			thunderbird -compose "to='${LIST_ADDRESS}',subject='[DONE] ${messages[$msgid]}',body='$DONE_MSG'"
			;;
		*)
			echo "Unknown Mail User Agent, you can use 'mailto' for the default behavior."
			;;
		esac
	done
fi

exit 0
